<!--
 * @Author: your name
 * @Date: 2021-11-02 11:20:32
 * @LastEditTime: 2021-11-02 11:30:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /websocket/index_1.html
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" name="" id="input">
    <ul id="uls"></ul>
    <script>
        const ws = new WebSocket('ws://localhost:9999/');
        // 连接的时候触发
        ws.onopen = function (scoket) {
            if(ws.readyState === 1) {
                console.log('连接成功')
            }
        }

        ws.onmessage = function (msg) {
            let li = document.createElement('LI')
            li.innerHTML = '<span>他说的：</span>' + msg.data;
            uls.appendChild(li)
        }
        
        input.onchange = function(e) {
            let li = document.createElement('LI')
            li.innerHTML = '<span>你说的：</span>' + e.target.value;
            uls.appendChild(li)
            ws.send(e.target.value)
        }
    </script>
</body>
</html>